package com.ruoyi.cookbook.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cookbook.mapper.RecipeMapper;
import com.ruoyi.cookbook.domain.Recipe;
import com.ruoyi.cookbook.service.IRecipeService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;

/**
 * 宠物管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class RecipeServiceImpl implements IRecipeService 
{
    @Autowired
    private RecipeMapper recipeMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询宠物
     * 
     * @param recipeId 宠物ID
     * @return 宠物
     */
    @Override
    public Recipe selectRecipeById(Long recipeId)
    {
        Recipe recipe = recipeMapper.selectRecipeById(recipeId);
        // 确保返回创建人信息
        if (recipe != null && (recipe.getCreateBy() == null || recipe.getCreateBy().isEmpty())) {
            recipe.setCreateBy("管理员");
        }
        return recipe;
    }

    /**
     * 查询宠物列表
     * 
     * @param recipe 宠物
     * @return 宠物
     */
    @Override
    public List<Recipe> selectRecipeList(Recipe recipe)
    {
        List<Recipe> recipeList = recipeMapper.selectRecipeList(recipe);
        // 确保每个宠物都有创建人信息
        for (Recipe r : recipeList) {
            if (r.getCreateBy() == null || r.getCreateBy().isEmpty()) {
                r.setCreateBy("管理员");
            }
        }
        return recipeList;
    }

    /**
     * 新增宠物
     * 
     * @param recipe 宠物
     * @return 结果
     */
    @Override
    public int insertRecipe(Recipe recipe)
    {
        recipe.setCreateTime(DateUtils.getNowDate());
        // 确保createBy字段已设置，若未设置则使用当前登录用户信息
        if (recipe.getCreateBy() == null || recipe.getCreateBy().isEmpty()) {
            // 这里使用SecurityUtils获取当前登录用户名称
            // 如果是微信小程序用户，应该使用昵称而非用户名
            try {
                String username = SecurityUtils.getUsername();
                SysUser user = userService.selectUserByUserName(username);
                if (user != null && user.getNickName() != null && !user.getNickName().isEmpty()) {
                    recipe.setCreateBy(user.getNickName());
                } else {
                    recipe.setCreateBy(username);
                }
            } catch (Exception e) {
                recipe.setCreateBy("匿名用户");
            }
        }
        return recipeMapper.insertRecipe(recipe);
    }

    /**
     * 修改宠物
     * 
     * @param recipe 宠物
     * @return 结果
     */
    @Override
    public int updateRecipe(Recipe recipe)
    {
        recipe.setUpdateTime(DateUtils.getNowDate());
        return recipeMapper.updateRecipe(recipe);
    }

    /**
     * 批量删除宠物
     * 
     * @param recipeIds 需要删除的宠物ID
     * @return 结果
     */
    @Override
    public int deleteRecipeByIds(Long[] recipeIds)
    {
        return recipeMapper.deleteRecipeByIds(recipeIds);
    }

    /**
     * 删除宠物信息
     * 
     * @param recipeId 宠物ID
     * @return 结果
     */
    @Override
    public int deleteRecipeById(Long recipeId)
    {
        return recipeMapper.deleteRecipeById(recipeId);
    }
} 